package com.cloudea.lotus.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;

public class GsonUtil {
    /**
     * 将object对象转成json格式字符串
     * @param object 要转化的对象
     * @return 转化后的json字符串
     */
    public static String toJson(Object object) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.disableHtmlEscaping();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        return gson.toJson(object);
    }

    /**
     * 高尔高尔夫
     * @param string 要解析的字符串
     * @param cls    要解析成的对象class
     * @param <T>    期望返回的对象类型
     * @return  返回T
     * @throws JsonSyntaxException
     */
    public static <T> T parse(String string, Class<T> cls) throws JsonSyntaxException {
        return new Gson().fromJson(string, cls);
    }
}